/*
表单校验：
1.用户名：单词字符，长度8到20位
2.密码：单词字符，长度8到20位
3.email：邮件格式
4.姓名：非空
5.手机号：手机号格式
6.出生日期：非空
7.验证码：非空
*/
let $username = $("#username");
let $password = $("#password");
let $confirmPassword = $("#confirm_password");
let $email = $("#email");
let $phonenumber = $("#phonenumber");
let $check = $("#check");
let $realname = $("#realname");
let $emailCheckCode = $("#emailCheckCode");
let $sendEmail = $("#sendEmail");
/*发送邮件时间*/
let countdown = 30

//图片点击事件
function getPic() {
    $("#checkCode").attr("src", "/checkCode.do?flag=" + Math.random());
};
//校验用户名
//单词字符，长度8到20位
function checkUsername() {
    //2.定义正则
    let reg_username = /\w{3,10}$/;
    //3.判断，给出提示信息
    let flag = reg_username.test($username.val().trim());
    if (flag) {
        //用户名合法
        $username.css("border", "");
    } else {
        //用户名非法,加一个红色边框
        $username.css("border", "1px solid red");
    }
    return flag;
}

//校验密码
function checkPassword() {
    //2.定义正则
    let reg_password = /\w{8,20}$/;
    //3.判断，给出提示信息
    let flag = reg_password.test($password.val().trim());
    if (flag) {
        //密码合法
        $password.css("border", "");
    } else {
        //密码非法,加一个红色边框
        $password.css("border", "1px solid red");
    }
    return flag;
}

//校验邮箱
function checkEmail() {
    //2.定义正则
    let reg_email = /^\w+@\w+\.\w+$/;

    //3.判断
    let flag = reg_email.test($email.val().trim());
    if (flag) {
        $email.css("border", "");
    } else {
        $email.css("border", "1px solid red");
    }
    return flag;
}

//手机号
function checkPhoneNumber() {
    //2.定义正则
    let reg_phone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

    //3.判断
    let flag = reg_phone.test($phonenumber.val().trim());
    if (flag) {
        $phonenumber.css("border", "");
    } else {
        $phonenumber.css("border", "1px solid red");
    }
    return flag;
}

//确认密码
function confirmPassword() {
    if ($password.val().trim() !== $confirmPassword.val().trim()) {
        $confirmPassword.css("border", "1px solid red")
        return false;
    }
    $confirmPassword.css("border", "")
    return true;
}

//确认验证码
function checkCheckCode() {
    //2.定义正则
    let reg_code = /^\w{4}$/;
    //3.判断
    let flag = reg_code.test($check.val().trim());
    if (flag) {
        $check.css("border", "");
    } else {
        $check.css("border", "1px solid red");
    }
    return flag;
}

//确认真实姓名
function checkRealName() {
    //2.匹配2-4位的汉字
    let reg_code = /^[\u4E00-\u9FA5\\s]{2,4}$/;
    //3.判断
    let flag = reg_code.test($realname.val().trim());
    if (flag) {
        $realname.css("border", "");
    } else {
        $realname.css("border", "1px solid red");
    }
    return flag;
}

//确认邮箱验证码
function checkEmailCheckCode() {
    //2.定义正则
    let reg_code = /^\w{6}$/;
    //3.判断
    let flag = reg_code.test($emailCheckCode.val().trim());
    if (flag) {
        $emailCheckCode.css("border", "");
    } else {
        $emailCheckCode.css("border", "1px solid red");
    }
    return flag;
}

function setTime() {
    if (countdown === 0) {
        $sendEmail.attr("disabled", false);
        $sendEmail.text("发送");
        countdown = 30;
    }
    $sendEmail.attr("disabled", true);
    $sendEmail.text("（" + countdown + "）");
    countdown--;
    setTimeout(setTime, 1000)
}

$sendEmail.on("click", function () {
    let email = $email.val().trim();
    if (checkUsername() && checkPassword() && checkEmail() &&
        confirmPassword() && checkPhoneNumber() && checkRealName()) {
        setTimeout(setTime, 1000)
        $.ajax({
            url: '/user/sendEmail',
            dataType: "text",
            data: {
                email: email
            },
            success: function (res) {
                alert(res)
            }
        })
    }
});
$(function () {
    //当表单提交时，调用所有的校验方法
    $("#registerForm").submit(function () {
        //1.发送数据到服务器
        if (checkUsername() && checkPassword() && confirmPassword()
            && checkPhoneNumber() && checkCheckCode()
            && checkRealName() && checkEmail() && checkEmailCheckCode()) {
            return true;
        }
        //2.不让页面跳转
        return false;
        //如果这个方法没有返回值，或者返回为true，则表单提交，如果返回为false，则表单不提交
    });
    //当某一个组件失去焦点是，调用对应的校验方法
    $username.blur(checkUsername);
    $password.blur(checkPassword);
    $email.blur(checkEmail);
    $check.blur(checkPhoneNumber);
    $confirmPassword.blur(confirmPassword)
    $check.blur(checkCheckCode)
    $realname.blur(checkRealName)
    $realname.blur(checkEmailCheckCode)
})